package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.AvailableCar;
import com.example.sprinbootdemo.entity.Car;
import com.example.sprinbootdemo.entity.RentCount;
import com.example.sprinbootdemo.service.CarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarController {

    private static final Logger logger = LoggerFactory.getLogger(CarController.class);

    @Autowired
    private CarService carService;

    // 获取所有的车辆记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<Car> list;
        try {
            list = carService.getAllCars();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取车辆记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取车辆记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据 CarNumber 获取车辆记录
    @GetMapping("/get/{carNumber}")
    public ResponseEntity<ApiResponse> getCarByCarNumber(@PathVariable int carNumber) {
        try {
            Car car = carService.getCarByCarNumber(carNumber);
            if (car == null) {
                ApiResponse response = new ApiResponse(0, "数据不存在", null);
                return ResponseEntity.status(404).body(response);
            }
            ApiResponse response = new ApiResponse(1, "成功", car);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取数据失败, CarNumber: {}", carNumber, e);
            ApiResponse response = new ApiResponse(0, "获取数据失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 添加新的车辆记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addCar(@RequestBody Car car) {
        try {
            logger.info("添加车辆记录: {}", car);
            carService.addCar(car);
            ApiResponse response = new ApiResponse(1, "车辆记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加车辆记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加车辆记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的车辆记录
    @DeleteMapping("/delete/{carNumber}")
    public ResponseEntity<ApiResponse> deleteCarByCarNumber(@PathVariable int carNumber) {
        try {
            logger.info("删除车辆记录, CarNumber: {}", carNumber);
            carService.deleteCarByCarNumber(carNumber);
            ApiResponse response = new ApiResponse(1, "车辆记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除车辆记录失败, CarNumber: {}", carNumber, e);
            ApiResponse response = new ApiResponse(0, "删除车辆记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的车辆记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateCarByCarNumber(@RequestBody Car car) {
        try {
            logger.info("更新车辆记录: {}", car);
            carService.updateCarByCarNumber(car);
            ApiResponse response = new ApiResponse(1, "车辆记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新车辆记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新车辆记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
    // 获取可租赁的车辆信息
    @GetMapping("/available")
    public ResponseEntity<ApiResponse> getAvailableCars() {
        List<AvailableCar> availableCars;
        try {
            availableCars = carService.getAvailableCars();
            ApiResponse response = new ApiResponse(1, "成功", availableCars);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取可租赁车辆信息失败", e);
            ApiResponse response = new ApiResponse(0, "获取可租赁车辆信息失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
    // 按车型查询可租赁的车辆信息
    @GetMapping("/available/byModel")
    public ResponseEntity<ApiResponse> getAvailableCarsByModelName(@RequestParam String modelName) {
        List<AvailableCar> availableCars;
        try {
            availableCars = carService.getAvailableCarsByModelName(modelName);
            ApiResponse response = new ApiResponse(1, "成功", availableCars);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("按车型获取可租赁车辆信息失败, ModelName: {}", modelName, e);
            ApiResponse response = new ApiResponse(0, "按车型获取可租赁车辆信息失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 获取不同类型车辆的租赁情况
    @GetMapping("/rentalcount")
    public ResponseEntity<ApiResponse> getRentalStatistics() {
        List<RentCount> rentCountList;
        try {
            rentCountList = carService.getRentCount();
            ApiResponse response = new ApiResponse(1, "成功", rentCountList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取租赁统计信息失败", e);
            ApiResponse response = new ApiResponse(0, "获取租赁统计信息失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}